//<MStar Software>
//******************************************************************************
// MStar Software
// Copyright (c) 2016 - 2016 MStar Semiconductor, Inc. All rights reserved.
// All software, firmware and related documentation herein ("MStar Software") are
// intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
// law, including, but not limited to, copyright law and international treaties.
// Any use, modification, reproduction, retransmission, or republication of all
// or part of MStar Software is expressly prohibited, unless prior written
// permission has been granted by MStar.
//
// By accessing, browsing and/or using MStar Software, you acknowledge that you
// have read, understood, and agree, to be bound by below terms ("Terms") and to
// comply with all applicable laws and regulations:
//
// 1. MStar shall retain any and all right, ownership and interest to MStar
//    Software and any modification/derivatives thereof.
//    No right, ownership, or interest to MStar Software and any
//    modification/derivatives thereof is transferred to you under Terms.
//
// 2. You understand that MStar Software might include, incorporate or be
//    supplied together with third party's software and the use of MStar
//    Software may require additional licenses from third parties.
//    Therefore, you hereby agree it is your sole responsibility to separately
//    obtain any and all third party right and license necessary for your use of
//    such third party's software.
//
// 3. MStar Software and any modification/derivatives thereof shall be deemed as
//    MStar's confidential information and you agree to keep MStar's
//    confidential information in strictest confidence and not disclose to any
//    third party.
//
// 4. MStar Software is provided on an "AS IS" basis without warranties of any
//    kind. Any warranties are hereby expressly disclaimed by MStar, including
//    without limitation, any warranties of merchantability, non-infringement of
//    intellectual property rights, fitness for a particular purpose, error free
//    and in conformity with any international standard.  You agree to waive any
//    claim against MStar for any loss, damage, cost or expense that you may
//    incur related to your use of MStar Software.
//    In no event shall MStar be liable for any direct, indirect, incidental or
//    consequential damages, including without limitation, lost of profit or
//    revenues, lost or damage of data, and unauthorized system use.
//    You agree that this Section 4 shall still apply without being affected
//    even if MStar Software has been modified by MStar in accordance with your
//    request or instruction for your use, except otherwise agreed by both
//    parties in writing.
//
// 5. If requested, MStar may from time to time provide technical supports or
//    services in relation with MStar Software to you for your use of
//    MStar Software in conjunction with your or your customer's product
//    ("Services").
//    You understand and agree that, except otherwise agreed by both parties in
//    writing, Services are provided on an "AS IS" basis and the warranty
//    disclaimer set forth in Section 4 above shall apply.
//
// 6. Nothing contained herein shall be construed as by implication, estoppels
//    or otherwise:
//    (a) conferring any license or right to use MStar name, trademark, service
//        mark, symbol or any other identification;
//    (b) obligating MStar or any of its affiliates to furnish any person,
//        including without limitation, you and your customers, any assistance
//        of any kind whatsoever, or any information; or
//    (c) conferring any license or right under any intellectual property right.
//
// 7. These terms shall be governed by and construed in accordance with the laws
//    of Taiwan, R.O.C., excluding its conflict of law rules.
//    Any and all dispute arising out hereof or related hereto shall be finally
//    settled by arbitration referred to the Chinese Arbitration Association,
//    Taipei in accordance with the ROC Arbitration Law and the Arbitration
//    Rules of the Association by three (3) arbitrators appointed in accordance
//    with the said Rules.
//    The place of arbitration shall be in Taipei, Taiwan and the language shall
//    be English.
//    The arbitration award shall be final and binding to both parties.
//
//******************************************************************************
//<MStar Software>

package com.mstar.android.media;

import android.util.Log;

public final class DVStreamInfo
{

    public DVStreamInfo()
    {
        Log.d(TAG, "DVStreamInfo constructed");
    }

    private static final String TAG = "DVStreamInfo";
    private static final boolean LOGD = true;

    static {
        System.loadLibrary("dvstreaminfo_jni");
    }

    /**
    * Indicates Dolby Vision stream profile is "dvav.per".
    */
    public static final int DV_STREAM_PROFILE_ID_DVAV_PER = 0x1;
    /**
    * Indicates Dolby Vision stream profile is "dvav.pen".
    */
    public static final int DV_STREAM_PROFILE_ID_DVAV_PEN = 0x2;
    /**
    * Indicates Dolby Vision stream profile is "dvhe.der".
    */
    public static final int DV_STREAM_PROFILE_ID_DVHE_DER = 0x4;
    /**
    * Indicates Dolby Vision stream profile is "dvhe.den".
    */
    public static final int DV_STREAM_PROFILE_ID_DVHE_DEN = 0x8;
    /**
    * Indicates Dolby Vision stream profile is "dvhe.dtr".
    */
    public static final int DV_STREAM_PROFILE_ID_DVHE_DTR = 0x10;
    /**
    * Indicates Dolby Vision stream profile is "dvhe.stn".
    */
    public static final int DV_STREAM_PROFILE_ID_DVHE_STN = 0x20;
    /**
    * Indicates Dolby Vision stream profile is "dvhe.dth".
    */
    public static final int DV_STREAM_PROFILE_ID_DVHE_DTH = 0x40;
    /**
    * Indicates Dolby Vision stream profile is Unsupported.
    */
    public static final int DV_STREAM_PROFILE_ID_UNSUPPORTED = 0x0;
    /**
    * Indicates Dolby Vision stream level is "HD24".
    */
    public static final int DV_STREAM_LEVEL_ID_HD24 = 0x1;
    /**
    * Indicates Dolby Vision stream level is "HD30".
    */
    public static final int DV_STREAM_LEVEL_ID_HD30 = 0x2;
    /**
    * Indicates Dolby Vision stream level is "FHD24".
    */
    public static final int DV_STREAM_LEVEL_ID_FHD24 = 0x4;
    /**
    * Indicates Dolby Vision stream level is "FHD30".
    */
    public static final int DV_STREAM_LEVEL_ID_FHD30 = 0x8;
    /**
    * Indicates Dolby Vision stream level is "FHD60".
    */
    public static final int DV_STREAM_LEVEL_ID_FHD60 = 0x10;
    /**
    * Indicates Dolby Vision stream level is "UHD24".
    */
    public static final int DV_STREAM_LEVEL_ID_UHD24 = 0x20;
    /**
    * Indicates Dolby Vision stream level is "UHD30".
    */
    public static final int DV_STREAM_LEVEL_ID_UHD30 = 0x40;
    /**
    * Indicates Dolby Vision stream level is "UHD48".
    */
    public static final int DV_STREAM_LEVEL_ID_UHD48 = 0x80;
    /**
    * Indicates Dolby Vision stream level is "UHD60".
    */
    public static final int DV_STREAM_LEVEL_ID_UHD60 = 0x100;
    /**
    * Indicates Dolby Vision stream level is unsupported.
    */
    public static final int DV_STREAM_LEVEL_ID_UNSUPPORTED = 0x0;
    /**
    * Indicates stream type is DolbyVision.
    */
    public static final int DV_STREAM_STREAM_TYPE_DV = 0x01;
    /**
    * Indicates stream type is SDR.
    */
    public static final int DV_STREAM_STREAM_TYPE_SDR = 0x02;
    /**
    * Indicates stream type HDR10.
    */
    public static final int DV_STREAM_STREAM_TYPE_HDR10 = 0x04;
    /**
    * Indicates stream type NULL
    */
    public static final int DV_STREAM_STREAM_TYPE_NULL = 0;
    /**
    * Indicates the API return status. Other error codes can be added.
    */
    public enum DVStatusCodes{
        DV_STATUS_SUCCESS(0), DV_ERR_UNSUPPORTED_PROFILE(1),
        DV_ERR_UNSUPPORTED_LEVEL(2), DV_ERR_UNSUPPORTED_STREAM_TYPE(3);

        private int status;
        private DVStatusCodes(int status){
            this.status = status;
        }
    };

    //Default stream type used to create media pipeline
    private int mInputStreamType = DV_STREAM_STREAM_TYPE_NULL;

    //Default DV Level used to create media pipeline
    private int mInputDVStreamLevel = DV_STREAM_LEVEL_ID_HD30;

    //Default DV profile used to create media pipeline
    private int mInputDVStreamProfile = DV_STREAM_PROFILE_ID_DVHE_DER;

    //Look up table to convert DV Profile id to DV profile code
    private final int mDVProfileIdMap[] = {DV_STREAM_PROFILE_ID_DVAV_PER,
                     DV_STREAM_PROFILE_ID_DVAV_PEN,
                     DV_STREAM_PROFILE_ID_DVHE_DER,
                     DV_STREAM_PROFILE_ID_DVHE_DEN,
                     DV_STREAM_PROFILE_ID_DVHE_DTR,
                     DV_STREAM_PROFILE_ID_DVHE_STN,
                     DV_STREAM_PROFILE_ID_DVHE_DTH};

    //Look up table to convert DV Level id to DV level code
    private final int mDVLevelIdMap[] = {  0, //level id starts from 1
                     DV_STREAM_LEVEL_ID_HD24,
                     DV_STREAM_LEVEL_ID_HD30,
                     DV_STREAM_LEVEL_ID_FHD24,
                     DV_STREAM_LEVEL_ID_FHD30,
                     DV_STREAM_LEVEL_ID_FHD60,
                     DV_STREAM_LEVEL_ID_UHD24,
                     DV_STREAM_LEVEL_ID_UHD30,
                     DV_STREAM_LEVEL_ID_UHD48,
                     DV_STREAM_LEVEL_ID_UHD60};

    //Look up table to convert supported stream types
    private final int mStreamTypeMap[] = {DV_STREAM_STREAM_TYPE_DV,
                     DV_STREAM_STREAM_TYPE_SDR,
                     DV_STREAM_STREAM_TYPE_HDR10};

    private native final int native_setDVProfile(int profileId);
    /**
     * API to set the Dolby Vision stream profile Id.
     * <p> The profileId indicates profile id of the Dolby Vision stream.
     * Possible values can be one of
     * {@link #DV_STREAM_PROFILE_ID_DVAV_PER},
     * {@link #DV_STREAM_PROFILE_ID_DVAV_PEN},
     * {@link #DV_STREAM_PROFILE_ID_DVHE_DER},
     * {@link #DV_STREAM_PROFILE_ID_DVHE_DEN},
     * {@link #DV_STREAM_PROFILE_ID_DVHE_DTR},
     * {@link #DV_STREAM_PROFILE_ID_DVHE_STN},
     * {@link #DV_STREAM_PROFILE_ID_DVHE_DTH},
     * {@link #DV_STREAM_PROFILE_ID_UNSUPPORTED}
     * <p> For non-DV stream, profile Id is set to {@link #DV_STREAM_PROFILE_ID_UNSUPPORTED}
         * @param profileId Dolby Vision stream profile information.
         * @return     {@link #DVStatusCodes}
         */
    public DVStatusCodes setDVProfile(int profileId)
    {
        switch (profileId)
        {
            case DV_STREAM_PROFILE_ID_DVAV_PER:
            case DV_STREAM_PROFILE_ID_DVAV_PEN:
            case DV_STREAM_PROFILE_ID_DVHE_DER:
            case DV_STREAM_PROFILE_ID_DVHE_DEN:
            case DV_STREAM_PROFILE_ID_DVHE_DTR:
            case DV_STREAM_PROFILE_ID_DVHE_STN:
            case DV_STREAM_PROFILE_ID_DVHE_DTH:
            case DV_STREAM_PROFILE_ID_UNSUPPORTED:
                mInputDVStreamProfile = profileId;
                break;
            default:
                return DVStatusCodes.DV_ERR_UNSUPPORTED_PROFILE;
        }

        //Set platform media pipeline stream type
        native_setDVProfile(mInputDVStreamProfile);
        return DVStatusCodes.DV_STATUS_SUCCESS;
    }

    private native final int native_setDVLevel(int LevelId);
    /**
     * API to set the Dolby Vision stream level information.
     * <p> The LevelId indicates the level of the Dolby Vision stream.
     * Possible values can be one of
     * {@link #DV_STREAM_LEVEL_ID_HD24},
     * {@link #DV_STREAM_LEVEL_ID_HD30},
     * {@link #DV_STREAM_LEVEL_ID_FHD24},
     * {@link #DV_STREAM_LEVEL_ID_FHD30},
     * {@link #DV_STREAM_LEVEL_ID_FHD60},
     * {@link #DV_STREAM_LEVEL_ID_UHD24},
     * {@link #DV_STREAM_LEVEL_ID_UHD30},
     * {@link #DV_STREAM_LEVEL_ID_UHD48},
     * {@link #DV_STREAM_LEVEL_ID_UHD60}
     * {@link #DV_STREAM_LEVEL_ID_UNSUPPORTED}
         * <p> For non-DV stream, profile Id is set to {@link #DV_ERR_UNSUPPORTED_LEVEL}
     * @param LevelId Dolby Vision stream level information.
         * @return     {@link #DVStatusCodes}
         */
    public DVStatusCodes setDVLevel(int levelId)
    {
        switch (levelId)
        {
            case DV_STREAM_LEVEL_ID_HD24:
            case DV_STREAM_LEVEL_ID_HD30:
            case DV_STREAM_LEVEL_ID_FHD24:
            case DV_STREAM_LEVEL_ID_FHD30:
            case DV_STREAM_LEVEL_ID_FHD60:
            case DV_STREAM_LEVEL_ID_UHD24:
            case DV_STREAM_LEVEL_ID_UHD30:
            case DV_STREAM_LEVEL_ID_UHD48:
            case DV_STREAM_LEVEL_ID_UHD60:
            case DV_STREAM_LEVEL_ID_UNSUPPORTED:
                mInputDVStreamLevel = levelId;
                break;
            default:
                return DVStatusCodes.DV_ERR_UNSUPPORTED_LEVEL;
        }

        //Set platform media pipeline stream type
        //set to level code as mentioned in DV profiles and levels doc
        native_setDVLevel(mInputDVStreamLevel);
        return DVStatusCodes.DV_STATUS_SUCCESS;
    }

    private native final int native_setStreamType(int streamType);
    /**
     * API to set the stream type.
     * <p> The streamType indicates the type of stream.
     * Possible values can be one of
     * {@link #DV_STREAM_STREAM_TYPE_DV},
     * {@link #DV_STREAM_STREAM_TYPE_SDR},
     * {@link #DV_STREAM_STREAM_TYPE_HDR10}
     * @param streamType Indicates the stream type
     * @return     {@link #DVStatusCodes}
         */
    public DVStatusCodes setStreamType(int streamType)
    {
        if (streamType == DV_STREAM_STREAM_TYPE_DV ||
            streamType == DV_STREAM_STREAM_TYPE_SDR ||
            streamType == DV_STREAM_STREAM_TYPE_HDR10)
        {
            //set to platform code for the stream type
            mInputStreamType = streamType;
            //Set platform media pipeline stream type
            native_setStreamType(streamType);
            return DVStatusCodes.DV_STATUS_SUCCESS;
        }
        else
        {
            return DVStatusCodes.DV_ERR_UNSUPPORTED_STREAM_TYPE;
        }
    }

    private native final int native_getSupportedDVProfiles();
    /**
     * API to query supported Dolby Vision stream profiles.
     * <p> Possible return value can be OR'ed combination of
     * {@link #DV_STREAM_PROFILE_ID_DVAV_PER},
     * {@link #DV_STREAM_PROFILE_ID_DVAV_PEN},
     * {@link #DV_STREAM_PROFILE_ID_DVHE_DER},
     * {@link #DV_STREAM_PROFILE_ID_DVHE_DEN},
     * {@link #DV_STREAM_PROFILE_ID_DVHE_DTR},
     * {@link #DV_STREAM_PROFILE_ID_DVHE_STN},
     * {@link #DV_STREAM_PROFILE_ID_DVHE_DTH},
     * <p> If DV stream not supported, profile Id is returned as {@link #DV_STREAM_PROFILE_ID_UNSUPPORTED}
         * @return    Dolby Vision stream profile support information.
         */
    public int getSupportedDVProfiles()
    {
        int DVSupportedProfiles = 0;

        //Read platform supported DV profiles from media info file (specific to each platform)
        DVSupportedProfiles = native_getSupportedDVProfiles();

        return DVSupportedProfiles;
    }

    private native final int native_getSupportedDVLevels(int dvProfileId);
    /**
     * API to query supported Dolby Vision stream levels for a given DV profile.
     * <p> Possible return values can be OR'ed combination of
     * {@link #DV_STREAM_LEVEL_ID_HD24},
     * {@link #DV_STREAM_LEVEL_ID_HD30},
     * {@link #DV_STREAM_LEVEL_ID_FHD24},
     * {@link #DV_STREAM_LEVEL_ID_FHD30},
     * {@link #DV_STREAM_LEVEL_ID_FHD60},
     * {@link #DV_STREAM_LEVEL_ID_UHD24},
     * {@link #DV_STREAM_LEVEL_ID_UHD30},
     * {@link #DV_STREAM_LEVEL_ID_UHD48},
     * {@link #DV_STREAM_LEVEL_ID_UHD60}
         * If DV stream not supported, Level Id is returned as {@link #DV_STREAM_PROFILE_ID_UNSUPPORTED}
     * @param dvProfileId Dolby Vision stream profile information.
         * @return     Dolby vision stream supported level info.
         */
    public int getSupportedDVLevels(int dvProfileId)
    {
        int DVSupportedLevels = 0;

        //Read platform supported DV Levels for given profileId from media info file (specific to each platform)
        DVSupportedLevels = native_getSupportedDVLevels(dvProfileId);

        return DVSupportedLevels;
    }

    private native final int native_getSupportedStreamTypes();
    /**
     * API to query supported stream types.
     * <p> Possible return values can be OR'ed combination of
     * {@link #DV_STREAM_STREAM_TYPE_DV},
     * {@link #DV_STREAM_STREAM_TYPE_SDR},
     * {@link #DV_STREAM_STREAM_TYPE_HDR10}
         * @return     Supported stream types.
         */
    public int getSupportedStreamTypes()
    {
        int supportedStreamTypes = 0;

        //Read platform supported DV Levels for given profileId from media info file (specific to each platform)
        supportedStreamTypes = native_getSupportedStreamTypes();

        return supportedStreamTypes;
    }

    private native final int native_getDVProfile();
    public int getDVProfile()
    {
        int dvprofile = 0;

        dvprofile = native_getDVProfile();

        return dvprofile;
    }

    private native final int native_getDVLevel();
    public int getDVLevel()
    {
        int dvlevel = 0;

        dvlevel = native_getDVLevel();

        return dvlevel;
    }

    private native final int native_getStreamType();
    public int getStreamType()
    {
        int dvtype = 0;

        dvtype = native_getStreamType();

        return dvtype;
    }

}
